home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / cvt100.zip / KEYIO.C < prev    next >
Text File  |  1988-08-01  |  15KB  |  395 lines

  1. #include <stdio.h>
  2. #include <dos.h>
  3. #include <bios.h>
  4.  
  5. #define ESC         0x1B          /* ASCII ESCape character */
  6. #define ESCAPE      0x11B         /* Keyboard ESCape scan code */
  7. #define DEL         0x7F          /* ASCII DELete character */
  8. #define BKSP        0xE08         /* Keyboard BacKSPace scan code */
  9. #define F1          0x3B00        /* Keyboard Function key 1 scan code */
  10. #define F2          0x3C00        /* Keyboard Function key 2 scan code */
  11. #define F3          0x3D00        /* Keyboard Function key 3 scan code */
  12. #define F4          0x3E00        /* Keyboard Function key 4 scan code */
  13. #define F5          0x3F00        /* Keyboard Function key 5 scan code */
  14. #define F6          0x4000        /* Keyboard Function key 6 scan code */
  15. #define F7          0x4100        /* Keyboard Function key 7 scan code */
  16. #define F8          0x4200        /* Keyboard Function key 8 scan code */
  17. #define F9          0x4300        /* Keyboard Function key 9 scan code */
  18. #define F10         0x4400        /* Keyboard Function key 10 scan code */
  19. #define UP          0x4800        /* Keyboard Up Arrow scan code */
  20. #define DOWN        0x5000        /* Keyboard Down Arrow scan code */
  21. #define LEFT        0x4B00        /* Keyboard Left Arrow scan code */
  22. #define RIGHT       0x4D00        /* Keyboard Right Arrow scan code */
  23. #define K7          0x4737        /* Keyboard Numeric 7 scan code */
  24. #define K8          0x4838        /* Keyboard Numeric 8 scan code */
  25. #define K9          0x4939        /* Keyboard Numeric 9 scan code */
  26. #define KDASH       0x372A        /* Keyboard Numeric Asteric scan code */
  27. #define K4          0x4B34        /* Keyboard Numeric 4 scan code */
  28. #define K5          0x4C35        /* Keyboard Numeric 5 scan code */
  29. #define K6          0x4D36        /* Keyboard Numeric 6 scan code */
  30. #define KCOMA       0x4A2D        /* Keyboard Numeric Dash(minus) scan code */
  31. #define K1          0x4F31        /* Keyboard Numeric 1 scan code */
  32. #define K2          0x5032        /* Keyboard Numeric 2 scan code */
  33. #define K3          0x5133        /* Keyboard Numeric 3 scan code */
  34. #define KENTR       0x4E2B        /* Keyboard Numeric + (plus) scan code */
  35. #define K0          0x5230        /* Keyboard Numeric 0 scan code */
  36. #define KDOT        0x532E        /* Keyboard Numeric Period scan code */
  37.  
  38. /*****************************************************************************/
  39. /* function prototypes                                                       */
  40.  
  41. void         Keyinit( void );     /* Initialize the keyboard system */
  42. int          ConChk( void );      /* Check the keyboard for keystrokes */
  43. unsigned int GetKey( void );      /* Retrieve a scan code from keyboard */
  44. int          DoKey( void );       /* Transmit a key sequence */
  45.  
  46. static void TransKey( unsigned ); /* Translate a keystroke to a sequence */
  47. static int TransNumKey(unsigned); /* Translate Numeric Keypad keystroke */
  48. static int TransApplKey(unsigned);/* Translate Application Keypad keystroke */
  49. static void SendBksp( void );     /* Transmit the backspace character */
  50. void   SetKeyPad( int );          /* Set the keypad to APPLICATION, NUMERIC */
  51. void   SetCursorKey( int );       /* Set the cursor key mode */
  52.  
  53.  
  54.  
  55.  
  56. /*****************************************************************************/
  57. /* Global variables                                                          */
  58.  
  59. unsigned char backspace;          /* Backspace/Delete translation flag */
  60. unsigned char keyclick;           /* Keyclick mode on/off */
  61. unsigned char applkeypad;         /* Current state of keypad */
  62.  
  63. void TTSetup( void );             /* Communications setup function */
  64. void VidSetup( void );            /* Video setup function */
  65. void KeySetup( void );            /* Keyboard setup function */
  66. void VTSetup( void );             /* VT emulation setup function */
  67. void FileSetup( void );           /* File system setup function */
  68.  
  69.  
  70. /*****************************************************************************/
  71. /* External variables                                                        */
  72.  
  73.  
  74. /*****************************************************************************/
  75. /* Local Static data                                                         */
  76.  
  77. static char cursorkey = '[';      /* Sequence character in cursor key */
  78. static union REGS regs;           /* Registers for int86 call */
  79.  
  80. /*****************************************************************************/
  81. /*****************************************************************************/
  82.  
  83.  
  84. /* K E Y I N I T -- Initialize the keyboard system */
  85.  
  86. void Keyinit() {
  87.  
  88.     delay(1);                     /* Initialize Turbo C delay function */
  89.     if (GetKeySetup() == 0) {     /* If no values were available from */
  90.         backspace = 0;            /* a setup file then provide defaults */
  91.         keyclick = 0;
  92.         applkeypad = 0;
  93.     }
  94. }
  95.  
  96.  
  97. /*  C O N C H K  --  Check if any key strokes are waiting, check hot keys */
  98.  
  99. ConChk()
  100. {
  101.     void (*setupfunct)();         /* Pointer to selected setup function */
  102.  
  103.     regs.h.ah = 0x1;              /* Use function 1 of interrupt 0x16 */
  104.     int86(0x16,®s,®s);      /* to check for waiting keystrokes */
  105.     if ( regs.x.flags & 0x40 )    /* If the zero flag is set then no keys */
  106.        return(0);
  107.     else {                        /* Else a key has been pressed */
  108.  
  109.        switch (regs.x.ax) {       /* Check to see if it was a hot key */
  110.  
  111.           case F5:
  112.              setupfunct = TTSetup;
  113.              break;
  114.           case F6:
  115.              setupfunct = VidSetup;
  116.              break;
  117.           case F7:
  118.              setupfunct = KeySetup;
  119.              break;
  120.           case F8:
  121.              setupfunct = VTSetup;
  122.              break;
  123.           case F9:
  124.              setupfunct = FileSetup;
  125.              break;
  126.           default:                /* If not a Setup key return 1 indicating */
  127.              return(1);              /* a keystroke is ready */
  128.        }
  129.        GetKey();                  /* Retrieve keystroke */
  130.        (*setupfunct)();           /* Call the selected setup function */
  131.        return(0);                 /* Return indicating no keystroke here yet */
  132.     }
  133. }
  134.  
  135.  
  136. /*  G E T K E Y  --  Return a keyboard scan code */
  137.  
  138. unsigned int GetKey() {
  139.     register unsigned int scancode;
  140.  
  141.     scancode = bioskey(0);        /* Get a keystroke, waits if none ready */
  142.  
  143.     if (keyclick) {               /* If keyclick flag is set */
  144.         sound(250);                  /* Turn on low frequency sound */
  145.         delay(5);                    /* Wait a short time period */
  146.         nosound();                   /* Turn off the sound */
  147.     }
  148.     return(scancode);             /* Return the retrieved scancode */
  149. }
  150.  
  151.  
  152.  
  153. /*  D O K E Y  --  Retrieve and interpret a keystroke */
  154.  
  155. DoKey() {
  156.     unsigned scancode;
  157.  
  158.     scancode = GetKey();          /* Get a keystroke, waits if none ready */
  159.  
  160.     if (scancode == F10)          /* Check for EXIT character */
  161.        return(-1);
  162.     else                          /* Else translate the pressed key */
  163.        TransKey(scancode);
  164.  
  165.     return(0);                    /* return success */
  166. }
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. /* T R A N S K E Y  -- translate a scancode into a keystroke sequence */
  174.  
  175. static void TransKey( unsigned key ) {
  176.  
  177.  
  178.     switch(key) {                 /* Evaluate this keyboard scan code */
  179.        case BKSP:                 /* Backspace pressed */
  180.             SendBksp();
  181.             break;
  182.        case F1:                   /* Function key 1 pressed */
  183.            ttoc( ESC );
  184.            ttoc( 'P' );
  185.            break;
  186.        case F2:                   /* Function key 2 pressed */
  187.            ttoc( ESC );
  188.            ttoc( 'Q' );
  189.            break;
  190.        case F3:                   /* Function key 3 pressed */
  191.            ttoc( ESC );
  192.            ttoc( 'R' );
  193.            break;
  194.        case F4:                   /* Function key 4 pressed */
  195.            ttoc( ESC );
  196.            ttoc( 'S' );
  197.            break;
  198.        case UP:                   /* Up Arrow